/**
 * AbstractDao.java 31/01/2010
 * Todos los derechos reservados.
 * Hudy Chan Rodriguez.
 * Ingeniero en Software
 * email: shogo_max@hotmail.com
 */
package org.company.ci.daos;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Clase abstracta para el soporte las operaciones basicas de los daos.
 * @version 1.0 31/01/2010
 * @author Hudy Chan Rodriguez
 */
public abstract class AbstractDao<BO> extends HibernateDaoSupport{
	
	private Class<BO> boClass;
	
	public void saveEntity(BO entity) throws Exception {
		getHibernateTemplate().save(entity);
	}
	
	public void updateEntity(BO entity) throws Exception {
		getHibernateTemplate().update(entity);
	}
	
	public void deleteEntity(BO entity) throws Exception {
		getHibernateTemplate().delete(entity);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<BO> getAllEntity(String condicion) throws Exception {
		List<BO> results = null;
		System.out.println("AbstractDAO --> obteniendo toda la lista de: "+ getBoClass().getSimpleName());
		String query = "from " + getBoClass().getSimpleName() + " " + condicion;
		System.out.println("AbstractDAO --> ejecutando query: "+query);
		results = getHibernateTemplate().find(query);
		
		return results;
		
	}
	
	public BO getBObyId(String id, String columnName) throws Exception {
		Long idBO = Long.parseLong(id);
		BO bo = null;
		String query = "from " + getBoClass().getSimpleName() + " bo where bo."+columnName+" = ?";
		
		System.out.println("AbstractDAO --> ejecutando query: "+query);
		
		List<BO> result = getHibernateTemplate().find(query,idBO);
		
		if(result.size() > 0){
			bo = result.get(0);
		}
		
		return bo;
		
	}
	
	public BO getBObyTextField(String field, String columnName) throws Exception {
		
		BO bo = null;
		String query = "from " + getBoClass().getSimpleName() + " bo where bo."+columnName+" = ?";
		
		System.out.println("AbstractDAO --> ejecutando query: "+query);
		
		List<BO> result = getHibernateTemplate().find(query,field);
		
		if(result.size() > 0){
			bo = result.get(0);
		}
		
		return bo;
		
	}
	
	public BO getBObyLongField(Long field, String columnName) throws Exception {
		
		BO bo = null;
		String query = "from " + getBoClass().getSimpleName() + " bo where bo."+columnName+" = ?";
		
		System.out.println("AbstractDAO --> ejecutando query: "+query);
		
		List<BO> result = getHibernateTemplate().find(query,field);
		
		if(result.size() > 0){
			bo = result.get(0);
		}
		
		return bo;
		
	}
	
	@SuppressWarnings("unchecked")
	private Class<BO> getBoClass(){
		if (boClass == null){
			ParameterizedType absDaoType = (ParameterizedType) this.getClass().getGenericSuperclass();
			boClass = (Class<BO>) absDaoType.getActualTypeArguments()[0];
		}
		
		return boClass;
	}
	
	public void clearSession() throws Exception {
		getHibernateTemplate().clear();
	}
	
	public void initialize(BO entity) throws Exception {
		this.getHibernateTemplate().initialize(entity);
	}
}
